
package com.trial.area.pictures.controller;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;

import com.trial.area.common.SpringBeansTool;
import com.trial.area.config.ConfigUtils;
import com.trial.area.pictures.entity.PicturesVO;
import com.trial.area.pictures.service.PicturesService;

/**
 * 系统图片信息Controller
 *
 * @author 朱国军
 * @since 1.0
 * @version 2015年12月27日 朱国军
 */
@Controller
public class PicturesServlet extends HttpServlet {

    /** FIXME */
    private static final long serialVersionUID = -6255633857230618187L;

    private Logger logger = LoggerFactory.getLogger(PicturesServlet.class);

    @Override
    public void destroy() {
        super.destroy();
    }
    
    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        PicturesService picturesService = SpringBeansTool.context.getBean(PicturesService.class);
        String id = request.getParameter("id");
        PicturesVO picturesVO = picturesService.findById(id);
        if (picturesVO != null) {

            String basepath = ConfigUtils.getStringValueByKey("picture.store_path");

            File file = new File(basepath + picturesVO.getPicturePath(), picturesVO.getPictureId() + "." + picturesVO.getPictureSuffix());
            if (file.exists()) { // 如果文件存在
                FileInputStream fin = null;
                ServletOutputStream out = null;
                try {
                    Path source = Paths.get(file.getPath());
                    String contentType = Files.probeContentType(source);
                    // 设置请求头参数
                    response.setContentType(contentType); // 设置返回内容格式
                    // 用该文件创建一个输入流
                    fin = new FileInputStream(file);
                    // 创建输出流
                    out = response.getOutputStream();
                    byte[] b = new byte[fin.available()];
                    fin.read(b);
                    out.write(b);
                    out.flush();
                } catch (Exception e) {
                    logger.error("读取图片信息出错,pictureId=" + id, e);
                } finally {
                    try {
                        if (fin != null) {
                            fin.close();
                        }
                        if (out != null) {
                            out.close();
                        }
                    } catch (Exception e) {
                        logger.error("读取图片时关闭流出错,pictureId=" + id, e);
                    }
                }
            } else {
                logger.error("图片文件不存在,pictureId=" + id);
            }
        } else {
            logger.error("数据库中找不到图片信息,pictureId=" + id);
        }
    }
    
    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
}
